//
//  WelcomeViewController.swift
//  SinaWeibo
//
//  Created by 郭平 on 15/10/12.
//  Copyright © 2015年 郭平. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        
        view.addSubview(headImageView)
        view.addSubview(infoLabel)
        
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        headImageView.centerX = SCREENW * 0.5
        headImageView.y = 200
        
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.headImageView.y = 100
            
            }) { (finish) -> Void in
              
                self.infoLabel.centerX = self.headImageView.centerX
                self.infoLabel.y = CGRectGetMaxY(self.headImageView.frame) + 6
                self.infoLabel.alpha = 0
                self.infoLabel.hidden = false
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    
                    self.infoLabel.alpha = 1
                    
                    }, completion: { (finish) -> Void in
                        
                        self.performSelector("toHome", withObject: nil, afterDelay:1)
                })
        }
    }
    
    @objc private func toHome() {
        
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
     
        delegate.window?.rootViewController = MainTabBarController()
    }
    
    // MARK: - 懒加载
    // 头像
    private lazy var headImageView: UIImageView = {
    
        let imageView = UIImageView()
        imageView.size = CGSizeMake(90, 90)
        
        imageView.layer.cornerRadius = imageView.height * 0.5
        imageView.layer.masksToBounds = true
        
        imageView.layer.borderColor = UIColor.darkGrayColor().CGColor
        imageView.layer.borderWidth = 2
        
        let url = NSURL(string: (UserAccount.loadUserAccount()?.avatar_large)!)
        imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        
        return imageView
    }()
    // 文字
    private lazy var infoLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "欢迎回来"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(16)
        label.sizeToFit()
    
        label.hidden = true
        
        return label
    }()

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
